#include "Shop.h"
#include "LoginSignUp.h"
#include <iostream>
#include <unistd.h>

void Shop::show()
{
    while(1)
    {
    system("clear");
    cout<<endl;
    std::cout<<"=======================商  店🛒=======================\n";
    std::cout<<"1、武器💣\n";
    std::cout<<"2、盾牌🧥\n";
    std::cout<<"3、药品💊\n";
    std::cout<<"4、退出❗\n";
    cout << "======================================================\n";

    std::cout<<"  请选择你要的商品种类：";
    char c = 0; 
    std::cin>>c;
    switch (c)
    {
        case '1':
        {
            showWeapon();
            break;
        }
        case '2':
        {
            showDefend();
            break;
        }
        case '3':
        {
            showMedicine();
            break;
        }
        case '4':
        {
            return;
        }
        default:
        {
            std::cout<<"按键输入错误, 请重新输入!\n";
            //sleep(2);
            break;
        }
    }
    }
}

void Shop::showWeapon()
{
    while(1)
    {
    system("clear");
    std::cout<<"==========================武器========================\n";
    std::cout<<"1、全村第一好的剑 ($20)\n";
    std::cout<<"2、全村第二好的剑 ($15)\n";
    std::cout<<"3、全村第三好的剑 ($10)\n";
    std::cout<<"4、退出             \n";
    cout << "======================================================\n";

    std::cout<<"您拥有金币"<<p.GetMoney()<<"个！ \n";
    std::cout<<"请选择你要购买的商品：";
    
    char c = 0;
    std::cin>>c;
        switch (c)
        {
            case '1':
            {
                system("clear");
                BuyWeapon(20,10,"全村第一好的剑");
                getchar();
                break;
            }
            case '2':
            {
                system("clear");
                BuyWeapon(15,5,"全村第二好的剑");
                getchar();
                break;
            }
            case '3':
            {
                system("clear");
                BuyWeapon(10,2,"全村第三好的剑");
                getchar();
                break;
            }
            case '4':
            {
                return;
            }
            default:
            {
                std::cout<<"按键输入错误, 请重新输入!\n";
                //sleep(2);
                break;
            }
        }
    }
}

void Shop::BuyWeapon(int WeaponMoney,int WeaponAttack,const std::string WeaponName)
{
    if(p.GetMoney() >= WeaponMoney)
    {
        p.SetMoney(p.GetMoney() - WeaponMoney);
        std::cout<<"您已购买 "<<WeaponName<<"! 剩余金币"<<p.GetMoney()<<"个！\n";
        printf("是否现在进行装备？ 'y' or 'n' \n");
        char d;
        std::cin>>d;
        switch (d)
        {
            case 'y':
            {
                p.SetAttack(p.GetAttack() + WeaponAttack);
                system("clear");
                std::cout<<"       已装备，按Enter返回商店\n";
                p.show();
                break;
            }
            case 'n':
            {
                auto weapon = std::make_shared<Weapon>();
                if(weapon)
                {
                    weapon->SetGoodsName(WeaponName);
                    weapon->SetAttack(WeaponAttack);
                    weapon->SetGoodsMoney(WeaponMoney);
                    p.GetBag()->Insert(weapon);
                    std::cout<<" 已将 "<<WeaponName<<" 放进背包！\n按Enter键返回商店\n";
                    break;
                }
                else
                {
                    std::cout << "内存分配失败，无法购买武器。\n";
                    return;
                }

            }
            default:
            {
                std::cout<<"按键输入错误, 请重新输入!\n";
                sleep(1);
                //break;
            }
        }
        getchar();
    }
    else
    {
        printf("金币不足\n");
    }
}

void Shop::showDefend()
{
    while(1)
    {
    system("clear");
    std::cout<<"==========================盾牌========================\n";
    std::cout<<"1、全村第一好的盾 ($15)\n";
    std::cout<<"2、全村第二好的盾 ($12)\n";
    std::cout<<"3、全村第三好的盾 ($8)\n";
    std::cout<<"4、退出             \n";
    std::cout<<"您拥有金币"<<p.GetMoney()<<"个！ \n";
    std::cout<<"请选择你要购买的商品：";
    
    char c;
    std::cin>>c;
    switch (c)
        {
            case '1':
            {
                system("clear");
                BuyDefend(15,10,"全村第一好的盾");
                getchar();
                break;
            }
            case '2':
            {
                system("clear");
                BuyDefend(12,5,"全村第二好的盾");
                getchar();
                break;
            }
            case '3':
            {
                system("clear");
                BuyDefend(8,2,"全村第三好的盾");
                getchar();
                break;
            }
            case '4':
            {
                return;
            }
            default:
            {
                std::cout<<"按键输入错误, 请重新输入!\n";
                //sleep(2);
                break;
            }
        }
    }
}

void Shop::BuyDefend(int DefendMoney, int DefendDefends, const std::string DefendName)
{
    if(p.GetMoney() >= DefendMoney)
    {
        p.SetMoney(p.GetMoney() - DefendMoney);
        std::cout<<"您已购买 "<<DefendName<<"! 剩余金币"<<p.GetMoney()<<"个！\n";
        printf("是否现在进行装备？ 'y' or 'n' \n");
        char d = 0;
        std::cin>>d;
        switch (d)
        {
            case 'y':
            {
                p.SetDefends(p.GetDefends() + DefendDefends);
                system("clear");
                std::cout<<"       已装备，按Enter返回商店\n";
                p.show();
                break;
            }
            case 'n':
            {
                auto defend = std::make_shared<Defend>();
                defend->SetGoodsName(DefendName);
                defend->SetDefends(DefendDefends);
                defend->SetGoodsMoney(DefendMoney);
                p.GetBag()->Insert(defend);
                std::cout<<" 已将 "<<DefendName<<" 放进背包！\n按任意键返回商店\n";
                break;
            }
            default:
            {
                std::cout<<"按键输入错误, 请重新输入!\n";
                sleep(2);
                break;
            }
        }
        getchar();
    }
    else
    {
        printf("金币不足\n");
    }
}

void Shop::showMedicine()
{
    while(1)
    {
    system("clear");
    std::cout<<"==========================药包========================\n";
    std::cout<<"1、全村第一好的药 ($10)\n";
    std::cout<<"2、全村第二好的药 ($5)\n";
    std::cout<<"3、全村第三好的药 ($1)\n";
    std::cout<<"4、退出             \n";
    std::cout<<"您拥有金币"<<p.GetMoney()<<"个！ \n";
    std::cout<<"请选择你要购买的商品：";
    
    char c = 0;
    std::cin>>c;
    switch (c)
        {
            case '1':
            {
                system("clear");
                BuyMedicine(10,20,"全村第一好的药");
                getchar();
                break;
            }
            case '2':
            {
                system("clear");
                BuyMedicine(5,10,"全村第二好的药");
                getchar();
                break;
            }
            case '3':
            {
                system("clear");
                BuyMedicine(1,5,"全村第三好的药");
                getchar();
                break;
            }
            case '4':
            {
                return;
            }
            default:
            {
                std::cout<<"按键输入错误, 请重新输入!\n";
                //sleep(2);
                break;
            }
        }
    }
}

void Shop::BuyMedicine(int MedicineMoney, int MedicineHP, const std::string MedicineName)
{
    if(p.GetMoney() >= MedicineMoney)
    {
        p.SetMoney(p.GetMoney() - MedicineMoney);
        std::cout<<"您已购买 "<<MedicineName<<"! 剩余金币"<<p.GetMoney()<<"个！\n";
        printf("是否现在进行装备？ 'y' or 'n' \n");
        char d = 0;
        std::cin>>d;
        switch (d)
        {
            case 'y':
            {
                p.SetHP(p.GetHP() + MedicineHP);
                system("clear");
                std::cout<<"       已装备，按Enter返回商店\n";
                p.show();
                break;
            }
            case 'n':
            {
                auto HP = std::make_shared<Medicine>();
                HP->SetGoodsName(MedicineName);
                HP->SetHP(MedicineHP);
                HP->SetGoodsMoney(MedicineMoney);
                p.GetBag()->Insert(HP);
                std::cout<<" 已将 "<<MedicineName<<" 放进背包！\n按Enter返回商店\n";
                break;
            }
            default:
            {
                std::cout<<"按键输入错误, 请重新输入!\n";
                sleep(2);
                break;
            }
        }
        getchar();
    }
    else
    {
        printf("金币不足\n");
    }
}
